package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JcMyCenterXufangFaQiEntity;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.jicai.view.fragment.MyCenterXuFangFaQiFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import org.greenrobot.eventbus.EventBus;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyCenterXuFangFraModel extends ViewModel {

    private MyCenterXuFangFaQiFragment fragment;
    private Map map;
    private int page = 1;
    public boolean hasNext = false;//是否有下一页

    public void getList(String groupBuyType,String groupbuyMode, String status, String goodsName, boolean isLoadMore) {
        map = new HashMap();
        page = isLoadMore ? page + 1 : 1;
        map.put("limit", 10);
        map.put("page", page);
        map.put("groupBuyType", groupBuyType);//1即报集采 2汇总集采 3企业专项
        map.put("status", status);//0全部 1集采中 2集采完成 4集采失败
        map.put("groupBuyMode", groupbuyMode);//1需方发起 2企业专项
        map.put("goodsName", goodsName);//商品名称
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getMyGroupBuyingList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<JcMyCenterXufangFaQiEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<JcMyCenterXufangFaQiEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    hasNext = data.getData().getPageSize() > page;//判断是否有下一页
                    fragment.showList(data.getData().getList(), isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }


    public void getH5Url(String id, int status) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("detail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id + "&type=" + status;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    //停止集采
    public void stopJiCai(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        JiCaiBaseApi.getApi().stopMyJicai(id).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(fragment.getContext(), "停止集采成功");
                    EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_MYCENTER_XUFANG_FAQI_TABNUM, ""));
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    //需方发起删除集采订单信息
    public void Delete(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
       JiCaiBaseApi.getApi().deleteGroupBuying(id).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(fragment.getContext(), "删除信息成功");
                    fragment.initData();
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void setActivity(MyCenterXuFangFaQiFragment myCenterXuFangFaQiFragment) {
        this.fragment = myCenterXuFangFaQiFragment;
    }
}
